package com.dwd.basic.service.impl;

import com.dwd.basic.constant.VerificationConstant;
import com.dwd.basic.exception.CustomException;
import com.dwd.basic.service.IVerification;
import com.dwd.basic.utils.SendMail;
import com.dwd.basic.utils.SendMsgUtils;
import com.dwd.basic.utils.StrUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.mail.MessagingException;
import java.util.concurrent.TimeUnit;

/**
 * @Description: TODO
 * @Author 汶狄
 * @Date 2021/1/16 14:51
 * @Version 1.0
 */
@Service
public class VerificationImpl implements IVerification {
    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private SendMail sendMail;

    /**
     * 注册发送验证码
     *
     * @param phone
     */
    @Override
    public void sendMobileCode(String phone) throws CustomException {
        sendVerification(phone, VerificationConstant.USER_REG);
    }

    /**
     * 绑定发送验证码
     *
     * @param phone
     */
    @Override
    public void
    sendBinderVerificationCode(String phone) throws CustomException {
        sendVerification(phone, VerificationConstant.WECHAT_BINDER);
    }

    /**
     * 发送邮箱验证码
     *
     * @param email
     */
    @Override
    public void sendEmailVerificationCode(String email) throws CustomException, MessagingException {
        String yanzhengma = sendVerification(email, VerificationConstant.USER_EMAIL);
        //发送验证码
        sendMail.sendMail(true,
                "注册验证码",
                "你的验证码为" + yanzhengma + "，五分钟有效",
                true,
                "1097466381@qq.com",
                "1097466381@qq.com");
    }

    /**
     * 抽取发送验证码公共方法
     *
     * @param phone  手机号码
     * @param suffix 存入reids的key的后缀
     */
    public String sendVerification(String phone, String suffix) throws CustomException {
        //随机产生四个字符
        String randomString = StrUtils.getComplexRandomString(4);
        //通过key获取value
        String value = (String) redisTemplate.opsForValue().get(phone + ":" + suffix);
        //查询redis是否有该值,如果有，说明该值还没到五分钟
        if (!StringUtils.isEmpty(value)) {
            //判断是否超过了一分钟
            Long befor = Long.valueOf(value.split(":")[1]);
            if (System.currentTimeMillis() - befor <= 60 * 1000) {
                throw new CustomException("一分钟只能发送一次验证码");
            }
            //超过一分钟，没超过五分钟,验证码依然是之前的
            //DKLy:1610784927335 截取验证码
            randomString = value.split(":")[0];
        }
        //存入redis
        redisTemplate.opsForValue().set(
                phone + ":" + suffix,
                randomString + ":" + System.currentTimeMillis(), 5, TimeUnit.MINUTES);
        //发送验证码
        String text = "汶狄告诉你，你的验证码为  " + randomString + "  请在五分钟之内完成注册";
        System.out.println(text);
//        SendMsgUtils.send(phone, text);
        //返回验证码
        return randomString;
    }
}
